﻿using XFControls.Utils;
using XFControls.XFPanels.XFPanelItems;
using System.Drawing;
using XFControls;
using System.IO;

namespace XFSenseUI.XFItems
{
    public enum FileBrowserItemType
    {
        Directory,
        File,
        UpOneLevel
    }
    public class SenseItemFileBrowserItem : SenseItemBase
    {
        public static SenseItemFileBrowserItem CreateUpOneLevel()
        {
            var ret = new SenseItemFileBrowserItem("..") {Type = FileBrowserItemType.UpOneLevel};

            return ret;
        }

        protected FileBrowserItemType Type;

        private string _path;
        private int _height;

        private SenseItemFileBrowserItem(string path)
        {
            SetupStyle();
            MainText = "..";
            _path = path;
        }

        public SenseItemFileBrowserItem(FileSystemInfo info)
        {
            SetupStyle();
            Type = FileBrowserItemType.File;
            _path = info.FullName;
            MainText = info.Name;

        }

        public SenseItemFileBrowserItem(DirectoryInfo info)
        {
            SetupStyle();
            Type = FileBrowserItemType.Directory;
            _path = info.FullName;
            MainText = info.Name;
        }

        private void SetupStyle()
        {
            ItemType = XFItemType.Clickable;
            Style = new XFItemStyle
            {
                BorderBottomColor = Color.FromArgb(189, 182, 189),
                DashStyleBottom = System.Drawing.Drawing2D.DashStyle.Dash,
                TextColor = Color.Black,
                TextFont = new Font(FontFamily.GenericSansSerif, 8, FontStyle.Regular),
                SelectedTextFont = new Font(FontFamily.GenericSansSerif, 8, FontStyle.Regular),
                SecondaryTextFont = new Font(FontFamily.GenericSansSerif, 7, FontStyle.Regular),
                SelectedSecondaryTextFont = new Font(FontFamily.GenericSansSerif, 7, FontStyle.Regular),
                SelectedBackgroundImageString = "item-select-background",
                SecondaryTextColor = Color.FromArgb(57, 52, 57),
                SelectedTextColor = Color.White,
                SelectedBackgroundColor = Color.FromArgb(43, 36, 43),
                SelectedSecondaryTextColor = Color.FromArgb(182, 178, 182),
                Padding = 11,
                PaddingBottom = 12,
                PaddingLeft = 10,
                PaddingRight = 16
            };
        }

        public string GetPath()
        {
            return _path;
        }

        public override XFItemClickResult GetClickResult()
        {
            OnClick();

            if (Type == FileBrowserItemType.File)
                return XFItemClickResult.Confirm;
            
            if (Type == FileBrowserItemType.UpOneLevel)
                return XFItemClickResult.SlideRight;

            SenseFileBrowserPanel panel = new SenseFileBrowserPanel(_path);
            panel.Filter = ((SenseFileBrowserPanel)Parent).Filter;
            panel.Attributes = Parent.Attributes;
            panel.ShowScrollbar(true);
            panel.ScrollbarColor = Parent.ScrollbarColor;

            ((SenseFileBrowserPanel)Parent).SetRightPanel(panel);

            var back = new XFItemBackHeader();
            panel.AddHeaderItem(back);

            if (Parent.Parent != null && Parent.Parent is XFPanelContainer)
            {
                var cont = Parent.Parent as XFPanelContainer;
                cont.SetHeaderLeftButton(new XFButtonBack());
            }

            return XFItemClickResult.SlideLeft;
        }

        public override int GetHeight()
        {
            if (!Visible)
                return 0;

            if (_height > 0)
                return _height;

            _height = CalcRecs(Style.PaddingLeft + _iconSize + Style.PaddingLeft);

            if (Buffer == null)
                Buffer = XFBitmapFactory.GetBitmap(GetWidth(false), _height);

            return _height;
        }

        public override void ItemPaint(Graphics g, int x, int y)
        {
            base.ItemPaint(g, x, y);

            Font fontMain = getCurrentFont(false);
            Font fontSecondary = getCurrentFont(true);
            Color colorMain = getCurrentFontColor(false);
            Color colorSecondary = getCurrentFontColor(true);


            if (!string.IsNullOrEmpty(MainText))
                g.DrawString(MainText, _mRec, Style, fontMain, colorMain);
            if (!string.IsNullOrEmpty(SecondaryText))
                g.DrawString(SecondaryText, _sRec, Style, fontSecondary, colorSecondary);

            g.DrawFileIcon(_path, new Rectangle(Style.PaddingLeft, Style.PaddingTop, _iconSize, _iconSize));
        }
    }
}
